//
//  Loan_OperatorAccreditViewController.m
//  JinFuThree
//
//  Created by 爱尚 on 2018/9/18.
//  Copyright © 2018年 毛韶谦. All rights reserved.
//

#import "Loan_OperatorAccreditViewController.h"
#import "Loan_OperatorAccreditCommonView.h"
#import "Loan_SignedAgreementViewController.h"

@interface Loan_OperatorAccreditViewController ()<UITextFieldDelegate>
@property(nonatomic,strong)UIScrollView *myScrollView;
@property(nonatomic,strong)Loan_OperatorAccreditCommonView *telView;
@property(nonatomic,strong)Loan_OperatorAccreditCommonView *pswView;
@property(nonatomic,strong)Loan_OperatorAccreditCommonView *statusView;
@property(nonatomic,strong)UIButton *xieyiAgreeBtn;
@property(nonatomic,strong)UIButton *xieyiBtn;
@property(nonatomic,strong)UIButton *nextBtn;

@end

@implementation Loan_OperatorAccreditViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"运营商授权";
    [self initViews];
}

-(void)initViews{
    
    UIView *contentView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidthMao, 600)];
    contentView.backgroundColor = ListMaoBackgroundColor;

    
    UIView *progressView = [[UIView alloc]init];
    progressView.backgroundColor = ThemeMaoColor;
    [self.view addSubview:progressView];
    [progressView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.mas_equalTo(self.view);
        make.width.mas_offset(@(ScreenWidthMao/6*5));
        make.height.mas_offset(@2);
    }];
    
    UILabel *label = [[UILabel alloc]init];
    label.text = @"只需本人手机号授权";
    label.textColor = HexColor(0xAAAAAA);
    label.font = [UIFont systemFontOfSize:13];
    [contentView addSubview:label];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(contentView.mas_left).offset(15);
        make.top.mas_equalTo(contentView.mas_top).offset(9);
        make.height.mas_offset(@14);
    }];
    
    
    _telView = [[Loan_OperatorAccreditCommonView alloc]init];
    _telView.titleLabel.text = @"手机号";
    _telView.writeTextField.placeholder = @"请输入手机号";
    _telView.writeTextField.delegate = self;
    _telView.writeTextField.tag = 1111;
    [contentView addSubview:_telView];
    [self.telView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(label.mas_bottom).offset(9);
        make.left.right.mas_equalTo(contentView);
        make.height.mas_offset(@56);
    }];
    
    _pswView = [[Loan_OperatorAccreditCommonView alloc]init];
    _pswView.titleLabel.text = @"服务密码";
    _pswView.writeTextField.placeholder = @"请输入手机服务密码";
    _pswView.writeTextField.delegate = self;
    _pswView.writeTextField.tag = 2222;
    [contentView addSubview:_pswView];
    [self.pswView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.height.mas_equalTo(self.telView);
        make.top.mas_equalTo(self.telView.mas_bottom);
    }];
    
    _statusView = [[Loan_OperatorAccreditCommonView alloc]init];
    _statusView.titleLabel.text = @"授权状态";
    _statusView.writeTextField.text = @"已授权";
    [contentView addSubview:_statusView];
    [self.statusView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.height.mas_equalTo(self.telView);
        make.top.mas_equalTo(self.pswView.mas_bottom);
    }];
    
    self.statusView.hidden = YES;
    
    _xieyiAgreeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [_xieyiAgreeBtn setTitle:@"本人已阅读并同意" forState:UIControlStateNormal];
    [_xieyiAgreeBtn setImage:[UIImage imageNamed:@"loan_btn_wgx"] forState:UIControlStateNormal];
    [_xieyiAgreeBtn setImage:[UIImage imageNamed:@"loan_btn_gx"] forState:UIControlStateSelected];
    [_xieyiAgreeBtn setTitleColor:ThemeMaoBlackColor forState:UIControlStateNormal];
    _xieyiAgreeBtn.titleLabel.font = UIFontMaoNormal;
    [_xieyiAgreeBtn addTarget:self action:@selector(agreeBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    [contentView addSubview:_xieyiAgreeBtn];
    [self.xieyiAgreeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(contentView.mas_left).offset(17);
        make.top.mas_equalTo(self.pswView.mas_bottom).offset(15);
        make.width.mas_offset(@140);
    }];
    
    _xieyiBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [_xieyiBtn setTitle:@"《运营商授权协议》" forState:UIControlStateNormal];
    [_xieyiBtn setTitleColor:ThemeMaoColor forState:UIControlStateNormal];
    _xieyiBtn.titleLabel.font = UIFontMaoNormal;
    [contentView addSubview:_xieyiBtn];
    [self.xieyiBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.xieyiAgreeBtn.mas_right);
        make.height.centerY.mas_equalTo(self.xieyiAgreeBtn);
    }];
    self.xieyiAgreeBtn.selected = YES;
    
    _nextBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [_nextBtn setTitle:@"下一步" forState:UIControlStateNormal];
    [_nextBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [_nextBtn setBackgroundColor:ThemeMaoColor];
    _nextBtn.layer.masksToBounds = YES;
    _nextBtn.titleLabel.font = [UIFont systemFontOfSize:15];
    [_nextBtn setBackgroundImage:[UIImage imageNamed:@"select_btn"] forState:UIControlStateNormal];
    _nextBtn.layer.cornerRadius = 4;
    [_nextBtn addTarget:self action:@selector(nextBtnAction) forControlEvents:UIControlEventTouchUpInside];
    [contentView addSubview:_nextBtn];
    [self.nextBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(contentView.mas_left).offset(20);
        make.right.mas_equalTo(contentView.mas_right).offset(-20);
        make.height.mas_offset(@44);
        make.top.mas_equalTo(self.xieyiBtn.mas_bottom).offset(25);
    }];
    
    UILabel *tipLbael111 = [[UILabel alloc]init];
    tipLbael111.textColor = HexColor(0xA3A1A1);
    tipLbael111.font = [UIFont systemFontOfSize:12];
    NSString *str = @"温馨提示：\n\n1.请授权本人实名制认证手机号\n\n2.登录成功后将收到运营商通知短信，无需回复";
    tipLbael111.numberOfLines = 0;
    tipLbael111.text = str;
    [contentView addSubview:tipLbael111];
    [tipLbael111 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(contentView.mas_left).offset(25);
        make.right.mas_equalTo(contentView.mas_right).offset(-25);
        make.top.mas_equalTo(self.nextBtn.mas_bottom).offset(20);
    }];
    

    
    
    self.myScrollView = [[UIScrollView alloc]initWithFrame:CGRectZero];
    self.myScrollView.contentSize = contentView.frame.size;
    self.myScrollView.showsVerticalScrollIndicator = NO;
    self.myScrollView.backgroundColor = ListMaoBackgroundColor;
    [self.myScrollView addSubview:contentView];
    [self.view addSubview:self.myScrollView];
    [self.myScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(self.view);
        make.top.mas_equalTo(self.view).offset(2);
        make.bottom.mas_equalTo(self.view).offset(SafeAreaBottomHeight);
    }];
    
    
    
}
-(void)nextBtnAction{
    
//    if (![HFQCommonTools isValidateMobile:self.telView.writeTextField.text]) {
//        [HUD_Mao showCharacterMessage:@"请填写正确的手机号"];
//        return ;
//    }else if (![HFQCommonTools isTrueVerificationCode:self.pswView.writeTextField.text]){
//        [HUD_Mao showCharacterMessage:@"请输入正确的4位验证码"];
//        return;
//    }
    Loan_SignedAgreementViewController *signVC = [[Loan_SignedAgreementViewController alloc]init];
    [self.navigationController pushViewController:signVC animated:YES];
    
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    
    return [self validateNumber:string];
}

- (BOOL)validateNumber:(NSString*)number {
    BOOL res = YES;
    NSCharacterSet* tmpSet = [NSCharacterSet characterSetWithCharactersInString:@"0123456789"];
    int i = 0;
    while (i < number.length) {
        NSString * string = [number substringWithRange:NSMakeRange(i, 1)];
        NSRange range = [string rangeOfCharacterFromSet:tmpSet];
        if (range.length == 0) {
            res = NO;
            break;
        }
        i++;
    }
    return res;
}



-(void)agreeBtnAction:(UIButton *)sender{
    sender.selected = !sender.selected;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
